package controller;

import gui.BoardPanel;
import gui.MainWindow;
import gui.SettingsPanel;

import java.io.FileNotFoundException;
import java.io.IOException;

import players.*;
import model.*;

public class Othello 
{ //class for human to play against computer

	OthelloBoard board;
	BoardPanel boardFrame;
	SettingsPanel settingsFrame;
	MainWindow main_window;
	
	Player random = new RandomPlayer();
	Player whitePlayer, 
    	   blackPlayer;
	
	public Othello() throws FileNotFoundException
	{	
		board = new OthelloBoard();
		
		boardFrame = new BoardPanel(board);                 // shows only the board
		settingsFrame = new SettingsPanel(boardFrame);      // shows the settings and interact with board
		main_window = new MainWindow(settingsFrame, boardFrame); // surrounds the board with stuff
		
		while(true)
		{			
			blackPlayer = settingsFrame.getPlayer(Field.BLACK); //will only return when button is pressed
			whitePlayer = settingsFrame.getPlayer(Field.WHITE); //will only return when button is pressed
			
			settingsFrame.button_pressed(false);                //reset the button
			
			playGame();
		}
	}
	
	public void playGame()
	{
		// initialize
		board.reset();
		if(settingsFrame.random_initialization())
			for (int i = 0; i < 2; i++) 
			{
				random.move(board, Field.BLACK);
				random.move(board, Field.WHITE);
			}

		// play the game
		boolean movedW = true, movedB = true;
		while ((movedW || movedB) && !settingsFrame.button_pressed()) 
		{
			main_window.updateBoard(Field.BLACK);
			movedB = blackPlayer.move(board, Field.BLACK);
			
			System.out.println(movedB ? "black moved." : "black can't move.");

			if ((!movedW && !movedB) || settingsFrame.button_pressed())
				break; // end of game

			main_window.updateBoard(Field.WHITE);
			movedW = whitePlayer.move(board, Field.WHITE);
			
			System.out.println(movedW ? "white moved." : "white can't move.");
		}
		if(!settingsFrame.button_pressed())
			main_window.updateFinalBoard();
	}
	
	public static void main(String[] arg) throws IOException
	{
		new Othello();
	}
}
